//
//  BaseViewController.m
//  Utils
//
//  Created by enghou on 2018/3/19.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "BaseViewController.h"
#import <SVProgressHUD.h>
#import "LoginViewController.h"
#import "ChooseAddressViewController.h"
@interface BaseViewController ()

@end

@implementation BaseViewController

-(void)commonFailBlockWithModel:(BaseModel *)model{
    if (model.code == 20000) {
        [self alertSome:model.msg sureAction:^{
            LoginViewController *login = [[LoginViewController alloc]init];
            [self.navigationController presentViewController:login animated:YES completion:nil];
        }];
    }else{
        [self alertSome:model.msg];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)beginLoading{
    [SVProgressHUD show];
}

-(void)endLoading{
    [SVProgressHUD dismiss];
}

-(void)alertSome:(NSString *)msg{
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alertController dismissViewControllerAnimated:YES completion:nil];
    });
}

-(void)alertSome:(NSString *)msg sureAction:(void(^)(void))act{
    [self alertSome:msg sureAction:act cancelAct:nil];
}

-(void)alertSome:(NSString *)msg sureAction:(void (^)(void))act cancelAct:(void(^)(void))cancel{
    [self alertSome:msg sureTitle:@"确定" sureAction:act cancelTitle:@"取消" cancelAct:cancel];
}

-(void)alertSome:(NSString *)msg sureTitle:(NSString *)sureTitle sureAction:(void(^)(void))sureAct cancelTitle:(NSString *)cancelTitle cancelAct:(void(^)(void))cancel{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (sureAct) {
            sureAct();
        }
    }];
    [alert addAction:sureAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancel) {
            cancel();
        }
    }];
    [alert addAction:cancelAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)push:(UIViewController *)vc{
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)chooseOrg{
    ChooseAddressViewController *choose = [[ChooseAddressViewController alloc]init];
    choose.theDelete = self;
    [self push:choose];
}

-(void)defaultBackButton{
    
}

@end
